package com.zrrd.blog.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zrrd.blog.api.entities.SysMenu;
import com.zrrd.blog.system.mapper.SysMenuMapper;
import com.zrrd.blog.system.req.SysMenuREQ;
import com.zrrd.blog.system.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.blog.util.base.Result;
import com.zrrd.blog.util.enums.ResultEnum;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单信息表 服务实现类
 * </p>
 *
 * @author chen
 * @since 2023-08-11
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Override
    public Result queryList(SysMenuREQ req) {
        QueryWrapper<SysMenu> wrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(req.getName())){
            wrapper.like("name",req.getName());
        }
        wrapper.orderByDesc("sort").orderByDesc("update_date");
        List<SysMenu> menuList=baseMapper.selectList(wrapper);
        //将根菜单封装为树状结构
        List<SysMenu> sysMenus=this.buildTree(menuList);
        return Result.ok(sysMenus);
    }

    private List<SysMenu> buildTree(List<SysMenu> menuList) {
        //获取根菜单
        List<SysMenu> rootMenuList=new ArrayList<>();
        for(SysMenu menu:menuList){
            if(menu.getParentId().equals("0")){
                rootMenuList.add(menu);
            }
        }
        //根菜单下的子菜单
        for(SysMenu menu:rootMenuList){
            //获取一个个根菜单，然后去获取对应的子菜单
            childrenMenu(menuList,menu);
        }
        return rootMenuList;
    }

    private SysMenu childrenMenu(List<SysMenu> menuList, SysMenu menu) {
        //存放menu菜单对象的所有子菜单
        List<SysMenu> children=new ArrayList<>();
        for(SysMenu m:menuList){
            //判断每个菜单的parent_id是否等于menu对象中的id
            if(m.getParentId().equals(menu.getId())){
                children.add(childrenMenu(menuList,m));
            }
        }
        menu.setChildren(children);
        return menu;
    }

    @Override
    public Result deleteById(String id) {
        //删除当前id的菜单
        baseMapper.deleteById(id);
        //删除子菜单
        LambdaQueryWrapper<SysMenu> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SysMenu::getParentId,id);
        baseMapper.delete(wrapper);
        return Result.ok();
    }

    @Override
    public Result findByUserId(String userId) {
        List<SysMenu> menuList=baseMapper.findByUserId(userId);
        //如果用户存在但没分配权限就会有空记录
        if(CollectionUtils.isEmpty(menuList) || menuList.get(0)==null){
            return Result.build(ResultEnum.MENU_NO);
        }
        //获取集合中的目录和菜单放到一个集合中，按钮放到另一个集合中
        List<SysMenu> dirMenuList =new ArrayList<>();
        List<SysMenu> buttonList=new ArrayList<>();
        for(SysMenu menu:menuList){
            if(menu.getType().equals(1) || menu.getType().equals(2)){
                dirMenuList.add(menu);
            }else{
                buttonList.add(menu);
            }
        }
        //封装菜单树
        List<SysMenu> menuTreeList=this.buildTree(dirMenuList);
        //响应数据
        Map<String,Object> data=new HashMap<>();
        data.put("menuTreeList",menuTreeList);
        data.put("buttonList",buttonList);
        return Result.ok(data);
    }

    @Override
    public List<SysMenu> findMenuListByUserIdForLogin(String userId) {
        List<SysMenu> menuList=baseMapper.findByUserId(userId);
        //如果用户存在但没分配权限就会有空记录
        if(CollectionUtils.isEmpty(menuList) || menuList.get(0)==null){
            return null;
        }
        return menuList;
    }

}
